package online.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.logging.log4j.LogManager;
import org.seasar.extension.timer.TimeoutManager;

import common.db.JdbcSource;
import common.db.dao.DaoUtil;

/**
 * ドライバ抹消リスナ
 * @author Tadashi Nakayama
 */
public final class S2DeregisterListener implements ServletContextListener {

	/**
	 * @see javax.servlet.ServletContextListener
	 * #contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(final ServletContextEvent arg0) {

		JdbcSource.deregister(this.getClass().getClassLoader());

		try {
			if (TimeoutManager.getInstance().stop(3000)) {
				return;
			}
		} catch (InterruptedException e) {
			Thread.interrupted();
			LogManager.getLogger().info(e.getMessage());
		}

		LogManager.getLogger(S2DeregisterListener.class).info("TimeoutManager is still alive.");
	}

	/**
	 * @see javax.servlet.ServletContextListener
	 * #contextInitialized(javax.servlet.ServletContextEvent)
	 */
	@Override
	public void contextInitialized(final ServletContextEvent arg0) {
		DaoUtil.addClassPath(this.getClass());
	}
}
